package com.xintonghua.bussiness.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.AddListPersonAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.AddServicePersonBean;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;

/**
 * 添加服务人员
 * Created by zhangxuemei on 2018/3/11.
 */

public class AddServicePersonActivity extends BaseActivity {
    @BindView(R.id.lv_person)
    ListView lvPerson;

    private AddListPersonAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_add_person);
        ButterKnife.bind(this);
        initUI();
        requestServicePersonList();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("添加服务人员");
        mAdapter = new AddListPersonAdapter(this,new ArrayList<AddServicePersonBean>());
        lvPerson.setAdapter(mAdapter);
    }

    private void requestServicePersonList(){
        HttpCent.getInstance(this).shopServicePerson(AuthManager.geteAuth().getShop_id(), this, MyState.requestOne);
    }

    @OnItemClick(R.id.lv_person)
    public void onItemClick(int position){
        AddServicePersonBean bean = mAdapter.getItem(position);
        mAdapter.notifyDataSetChanged();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AddLogActivity.SERVICE_KEY, bean);
        setResult(0, new Intent().putExtras(bundle));
        finish();
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                List<AddServicePersonBean> list = JSONArray.parseArray(data.toString(), AddServicePersonBean.class);
                if (list != null && list.size() > 0) {
                    mAdapter.setList(list);
                    mAdapter.notifyDataSetChanged();
                }else{
                    mAdapter.getList().clear();
                    mAdapter.notifyDataSetChanged();
                }
                break;
        }
    }
}
